package virtualstock.register;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import virtualstock.exception.InvalidParameterException;
import virtualstock.request.PlayerController;
import virtualstock.util.PlayerDetails;

/**
 * Servlet implementation class PlayerRegisterServlet
 */
public class PlayerRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    @EJB
	private PlayerController playerController; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayerRegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	PrintWriter out = response.getWriter();
    	
    	String email = (String) request.getParameter("email");
    	String password = (String) request.getParameter("password");
    	String password2 = (String) request.getParameter("password2");
    	String firstName = (String) request.getParameter("firstname");
    	String lastName = (String) request.getParameter("lastname");
    	System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    	System.out.println(email+" "+password+" "+password2+" "+firstName+" "+lastName);
    	
    	if(!password.equals(password2)){
    		out.println("two passwds do not match!");
    		response.sendRedirect("error\\errorRegisterPassword.html");
    	}
    	
    	try {
    		List<PlayerDetails> player = playerController.getPlayersByEmail(email);
    		if(player != null){
    			out.println("email exists");
    			response.sendRedirect("error\\errorEmailExist.html");
    		}else{
    			PlayerDetails details = new PlayerDetails(email, password, firstName, lastName);
    			Long playerID = playerController.createPlayer(details);
    			details.setPlayerID(playerID);
    			out.println("Register Succefully!");
    			out.println("The page will be redirected to login page in 5 seconds");
    			try {
    				TimeUnit.SECONDS.sleep(2);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			response.sendRedirect("login.html");
    		}
		} catch (InvalidParameterException e) {
			out.print("InvalidParameterException");
			e.printStackTrace();
		}
		
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
